package thefirst.controller;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import thefirst.support.model.HelpService;
import thefirst.support.model.HelpVO;
import thefirst.support.model.NoticeService;
import thefirst.support.model.NoticeVO;
@Controller
public class SupportController {
	private String path="C:\\java-kosta\\workspace\\project-thrfirst\\whereismyteacher-b0.01\\WebContent\\upload\\";
	@Resource(name="noticeServiceImpl")
	private NoticeService noticeService;
	@Resource(name="helpServiceImpl")
	private HelpService helpService;
	/**
	 * 공지사항 쓰기
	 * 새로고침을 막기위해 redirect
	 * @param request
	 * @param nvo
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/writeNotice.do")
	public ModelAndView writeNotice(NoticeVO nvo) throws SQLException, IllegalStateException, IOException{
		System.out.println("controller:"+nvo);
		MultipartFile mFile=nvo.getUploadFile();
		if(mFile!=null&&mFile.isEmpty()==false){
			nvo.setOrgFileName(mFile.getOriginalFilename());
			String uploadName=System.currentTimeMillis()+"_"+nvo.getOrgFileName();
			nvo.setSavFileName(uploadName);
			File copyFile=new File(path+uploadName);
			mFile.transferTo(copyFile);
		}
		noticeService.writeNotice(nvo);
		return new ModelAndView("redirect:/showNoticeInfoNoHits.do?no="+nvo.getNo());
	}
	/**
	 * 조회수가 올라가지 않고 공지사항 정보 가져오는 메서드
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/showNoticeInfoNoHits.do")
	public ModelAndView showNoticeInfoNoHits(int no) throws SQLException{
		return new ModelAndView("support/notice/showNoticeInfo.notice","nvo",noticeService.showNoticeInfoNoHits(no));
	}
	/**
	 * 글쓰기 페이지로 이동
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/writeNoticePage.do")
	public String writePage(HttpServletRequest request,HttpServletResponse response) throws Exception{
		return "support/notice/writeNoticePage.notice";
	}
	/**
	 * 공지사항 목록에서 글보기 페이지로 이동
	 * 조회수 증가
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/showNoticeInfo.do")
	public ModelAndView showNoticeInfo(int no) throws SQLException{
		System.out.println("showNoticeInfo:"+no);
		NoticeVO nvo=noticeService.showNoticeInfo(no);
		System.out.println("showNoticeInfo:"+nvo);
		return new ModelAndView("support/notice/showNoticeInfo.notice","nvo",nvo);
	}
	/**
	 * 공지사항 업데이트 페이지로 이동
	 * no로 글의 정보를 가져온다.
	 * @param no
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateNoticePage.do")
	public ModelAndView updatePage(int no) throws Exception{
		return new ModelAndView("support/notice/updateNoticePage.notice","nvo",noticeService.showNoticeInfoNoHits(no));
	}
	/**
	 * 공지사항 목록 가져오는 메서드
	 * 페이지 당 10개(rownum/10)
	 * @param pageNo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/noticeList.do")
	public ModelAndView getNoticeList(String pageNo) throws SQLException{
		return new ModelAndView("support/notice/noticeList.notice","list",noticeService.getNoticeList(pageNo));
	}
	/**
	 * 공지사항 글 수정
	 * @param nvo
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/updateNotice.do")
	public ModelAndView updateNotice(NoticeVO nvo) throws SQLException, IllegalStateException, IOException{
		MultipartFile mFile=nvo.getUploadFile();
		if(mFile!=null&&mFile.isEmpty()==false){
			nvo.setOrgFileName(mFile.getOriginalFilename());
			String uploadName=System.currentTimeMillis()+"_"+nvo.getOrgFileName();
			nvo.setSavFileName(uploadName);
			File copyFile=new File(path+uploadName);
			mFile.transferTo(copyFile);
		}
		int result=noticeService.updateNotice(nvo);
		System.out.println("updateNotice result:"+result);
		String mess="support/notice/showNoticeInfo.notice";
		if(result==0){
			mess="/WEB-INF/error";
		}
		return new ModelAndView(mess,"nvo",noticeService.showNoticeInfoNoHits(nvo.getNo()));
	}
	/**
	 * 공지사항 글 삭제
	 * 조건 : 파일이 존재할 경우 삭제
	 * @param no
	 * @param pageNo
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/deleteNotice.do")
	public ModelAndView deleteNotice(int no) throws Exception{
		NoticeVO nvo=noticeService.showNoticeInfoNoHits(no);
		String uploadFile=nvo.getSavFileName();
		if(uploadFile!=null||!uploadFile.equals("")){
			noticeService.deleteFile(path+uploadFile);
		}
		noticeService.deleteNotice(no);
		return new ModelAndView("support/notice/noticeList.notice","list",noticeService.getNoticeList("1"));
	}
	/**
	 * 파일 다운로드
	 * util.DownloadView에서 request.getParameter("savFileName") <- 값 확인
	 * @param savFileName
	 * @return
	 */
	@RequestMapping("/downloadNoticeFile.do")
	public ModelAndView downloadNoticeFile(String savFileName){
		System.out.println("savFileName:"+savFileName);
		HashMap map=new HashMap();
		map.put("path", path);
		return new ModelAndView("downloadView",map);
	}
	@RequestMapping("/deleteFileForUpdate.do")
	@ResponseBody
	public int deleteFileForUpdate(int no) throws Exception{
		System.out.println("json:"+no);
		NoticeVO nvo=noticeService.showNoticeInfoNoHits(no);
		String uploadFile=nvo.getSavFileName();
		if(uploadFile!=null||!uploadFile.equals("")){
			noticeService.deleteFile(path+uploadFile);
		}
		int result=noticeService.updateNoticeNoFile(no);
		return result;
	}
	@RequestMapping("/writeHelp.do")
	public ModelAndView writeHelp(HttpServletRequest request, HelpVO hvo) throws SQLException{
		helpService.writeHelp(hvo);
		return new ModelAndView("redirect:/showHelpInfo.do?no="+hvo.getNo());
	}
	@RequestMapping("/showHelpInfoNoHits.do")
	public ModelAndView showHelpInfoNoHits(int no) throws SQLException{
		return new ModelAndView("showHelpInfo","hvo",helpService.showHelpInfoNoHits(no));
	}
	
	
}
